<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="4" :sm="6">
            <a-form-item label="执行状态">
              <a-select v-model="queryParam.Status">
                <a-select-option key="-1" value="-1" title="全部">
                  <a-row>
                    <a-col>全部</a-col>
                  </a-row>
                </a-select-option>
                <a-select-option key="0" value="0" title="待下发">
                  <a-row>
                    <a-col>待下发</a-col>
                  </a-row>
                </a-select-option>
                <a-select-option key="1" value="1" title="执行中">
                  <a-row>
                    <a-col>执行中</a-col>
                  </a-row>
                </a-select-option>
                <a-select-option key="2" value="2" title="已完成">
                  <a-row>
                    <a-col>已完成</a-col>
                  </a-row>
                </a-select-option>
                <a-select-option key="3" value="3" title="已取消">
                  <a-row>
                    <a-col>已取消</a-col>
                  </a-row>
                </a-select-option>
                <a-select-option key="4" value="4" title="挂起">
                  <a-row>
                    <a-col>挂起</a-col>
                  </a-row>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="6">
            <a-form-item label="订单编号">
              <a-input v-model="queryParam.WipOrderNo" placeholder="订单编号" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="计划日期">
              <a-range-picker v-model="queryParam.ScheduledStartDate">
                <a-icon slot="suffixIcon" type="coffee" />
              </a-range-picker>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" v-action:Query @click="()=>{this.$refs.table.refresh()}">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table ref="table" size="default" rowKey="Id" :columns="columns" :data="loadData" showPagination="auto">
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="record.Status===0" v-action:Update @click="handleEdit(record,1)">下发</a>
          <a-divider v-if="record.Status===0" v-action:Update type="vertical" />

          <a v-action:Update v-if="record.Status===1" @click="handleEdit(record,4)">挂起</a>
          <a-divider v-action:Update v-if="record.Status===1" type="vertical" />

          <a v-action:Update v-if="record.Status===1" @click="handleEdit(record,3)">取消</a>
          <a-divider v-action:Update v-if="record.Status===1" type="vertical" />

          <a v-action:Update v-if="record.Status===4" @click="handleEdit(record,1)">重启</a>
          <a-divider v-action:Update v-if="record.Status===4" type="vertical" />

          <a v-action:Update @click="handlePlanSequence(record)">工序</a>
          <a-divider v-action:Update type="vertical" />
          <a v-action:Update @click="handlePlanExecuted(record)">报工</a>
          <a-divider v-action:Update type="vertical" />
          <a v-action:Update @click="handleDetails(record)">详情</a>
        </template>
      </span>
    </s-table>
    <DetailsForm ref="detailsForm" @Success="()=>{this.$refs.table.refresh()}"></DetailsForm>
    <PlanSequenceForm ref="planSequenceForm"></PlanSequenceForm>
    <PlanExecutedForm ref="planExecutedForm"></PlanExecutedForm>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import MainSvc from '@/api/Exec/PlanSvc'
import DetailsForm from './Details'
import PlanSequenceForm from '../OprSequence/PlanSequence'
import PlanExecutedForm from '../StationExecuted/PlanExecuted'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'

const columns = [
  { title: '订单编号', dataIndex: 'WipOrderNo', sorter: true },
  { title: '订单物料', dataIndex: 'ProductNo', sorter: true },
  { title: '订单物料描述', dataIndex: 'ProductName', sorter: true },
  { title: '订单物料简码', dataIndex: 'ProductAlias', sorter: true },
  { title: '数量', dataIndex: 'Quantity', sorter: true },
  { title: '计划开始时间', dataIndex: 'ScheduledStartDate', sorter: true, customRender: (value) => { return moment(value).format('yyyy-MM-DD') } },
  { title: '实际下发时间', dataIndex: 'IssuedTime', sorter: true, customRender: (value) => {
      if (value !== null) {
        return moment(value).format('YYYY-MM-DD HH:mm:ss')
      } else {
        return ''
      }
    }
  },
  { title: '订单状态', dataIndex: 'ProgressStatus', sorter: true, customRender: (value) => {
    if (value === '110') {
      return '已接收'
    } else if (value === '120') {
      return '进行中'
    } else if (value === '130') {
      return '已完工'
    } else if (value === '140') {
      return '已删除'
    } else if (value === '150') {
      return '强制关闭'
    } else {
      return value
    }
   } }, { title: '执行状态', dataIndex: 'Status', sorter: true, customRender: (value) => {
    if (value === 0) {
      return '待下发'
    } else if (value === 1) {
      return '执行中'
    } else if (value === 2) {
      return '已完成'
    } else if (value === 3) {
      return '已取消'
    } else if (value === 4) {
      return '挂起'
    } else {
      return value
    }
   } },
  { title: '操作', dataIndex: 'action', width: '260px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    STable,
    MainSvc,
    EnumSelect,
    EnumName,
    DetailsForm,
    PlanSequenceForm,
    PlanExecutedForm
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: { Status: '-1', WipOrderNo: '', ScheduledStartDate: ['', ''] },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var _query = Object.assign({}, { ...this.queryParam })
        for (const key in _query) {
          if (moment.isMoment(_query[key])) {
            _query[key] = _query[key].format('YYYY-MM-DD')
          }
        }
        const requestParameters = Object.assign({ sortField: 'ModifyTime', sortOrder: 'desc', Search: _query }, parameter)
        console.log('loadData request parameters:', requestParameters)
        return MainSvc.GetPage(requestParameters)
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    moment,
    handleEdit(record, status) {
      this.loading = true
      record.status = status

      var stitle = ''
      if (status === 1) {
        stitle = '下发'
      } else if (status === 3) {
        stitle = '取消'
      } else if (status === 4) {
        stitle = '挂起'
      }

      this.$confirm({
        title: '确认' + stitle + '吗?',
        onOk() {
          MainSvc.Save(record).then(result => {
          this.loading = false
          if (result.Success) {
            this.$message.success(result.Msg)
            this.$refs.table.refresh()
            this.visible = false
          } else {
            this.$message.error(result.Msg)
          }
        })
        }
      })
    },
    handleDetails (record) {
      this.mdl = null
      this.visible = true
      this.$refs.detailsForm.openForm(record, '查看详情')
    },
    handlePlanSequence(record) {
      this.mdl = null
      this.visible = true
      this.$refs.planSequenceForm.openForm(record, '查看订单工序信息')
    },
    handlePlanExecuted(record) {
      this.mdl = null
      this.visible = true
      this.$refs.planExecutedForm.openForm(record, '查看订单报工信息')
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      this.queryParam = { Status: '-1', WipOrderNo: '', ScheduledStartDate: ['', ''] }
    },
    handleDelete(rows) {
      var thisObj = this
      var ids = rows.map(value => value.Id)
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            MainSvc.Delete(ids).then(result => {
              resolve()
              if (result.Success) {
                thisObj.$message.success('操作成功!')
                thisObj.$refs.table.refresh()
              } else {
                thisObj.$message.error(result.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
